<template>
  <div class="reset-container">
    <div class="reset-box">
      <h2>重置密码</h2>
      
      <el-form
        ref="resetFormRef"
        :model="resetForm"
        :rules="resetRules"
        class="reset-form"
      >
        <el-form-item prop="phone">
          <el-input
            v-model="resetForm.phone"
            placeholder="请输入手机号"
            prefix-icon="Phone"
            autocomplete="off"
            autocorrect="off"
            autocapitalize="off"
          />
        </el-form-item>
        
        <el-form-item prop="smsCode">
          <div class="sms-container">
            <el-input
              v-model="resetForm.smsCode"
              placeholder="请输入短信验证码"
              style="width: 60%"
              autocomplete="off"
              autocorrect="off"
              autocapitalize="off"
            />
            <el-button
              type="primary"
              :disabled="!resetForm.phone || smsCountdown > 0"
              @click="sendSms"
            >
              {{ smsCountdown > 0 ? `${smsCountdown}s` : '发送验证码' }}
            </el-button>
          </div>
        </el-form-item>
        
        <el-form-item prop="newPassword">
          <el-input
            v-model="resetForm.newPassword"
            type="password"
            placeholder="请输入新密码"
            prefix-icon="Lock"
            show-password
            autocomplete="new-password"
          />
        </el-form-item>
        
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="resetForm.confirmPassword"
            type="password"
            placeholder="请确认新密码"
            prefix-icon="Lock"
            show-password
            autocomplete="new-password"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            :loading="resetLoading"
            style="width: 100%"
            @click="handleResetPassword"
          >
            重置密码
          </el-button>
        </el-form-item>
        
        <div class="reset-links">
          <el-link type="primary" @click="$router.push('/login')">
            返回登录
          </el-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'

const router = useRouter()

const resetFormRef = ref()
const resetLoading = ref(false)
const smsCountdown = ref(0)

const resetForm = reactive({
  phone: '',
  smsCode: '',
  newPassword: '',
  confirmPassword: ''
})

const resetRules = {
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  smsCode: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { pattern: /^\d{4}$/, message: '验证码为4位数字', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== resetForm.newPassword) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

// 发送短信验证码
const sendSms = async () => {
  if (!resetForm.phone) {
    ElMessage.warning('请先输入手机号')
    return
  }
  
  try {
    const response = await request.post('/sendSms', {
      phone: resetForm.phone
    })
    
    if (response.data.code === 200) {
      ElMessage.success('验证码发送成功')
      // 开始倒计时
      smsCountdown.value = 60
      const timer = setInterval(() => {
        smsCountdown.value--
        if (smsCountdown.value <= 0) {
          clearInterval(timer)
        }
      }, 1000)
    } else {
      ElMessage.error(response.data.message || '发送失败')
    }
  } catch (error) {
    console.error('发送短信失败:', error)
    ElMessage.error('发送失败')
  }
}

// 重置密码处理
const handleResetPassword = async () => {
  if (!resetFormRef.value) return
  
  try {
    await resetFormRef.value.validate()
    resetLoading.value = true
    
    const response = await request.post('/resetPassword', {
      phone: resetForm.phone,
      smsCode: resetForm.smsCode,
      newPassword: resetForm.newPassword
    })
    
    if (response.data.code === 200) {
      ElMessage.success('密码重置成功')
      // 清空表单
      resetForm.phone = ''
      resetForm.smsCode = ''
      resetForm.newPassword = ''
      resetForm.confirmPassword = ''
      // 返回登录页面
      router.push('/login')
    } else {
      ElMessage.error(response.data.message || '重置失败')
    }
  } catch (error) {
    console.error('重置密码失败:', error)
    ElMessage.error('重置失败')
  } finally {
    resetLoading.value = false
  }
}
</script>

<style scoped>
.reset-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.reset-box {
  width: 400px;
  padding: 40px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
}

.reset-box h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
}

.reset-form {
  width: 100%;
}

.sms-container {
  display: flex;
  gap: 10px;
  align-items: center;
}

.reset-links {
  text-align: center;
  margin-top: 20px;
}

/*
  测试用户信息：
  admin  13800138000
  test   15811238888
*/
</style> 